<!--
 * @Author: Li Zengkun
 * @Date: 2022-09-13 19:30:35
 * @LastEditors: Li Zengkun
 * @LastEditTime: 2022-09-18 16:31:14
 * @Description: 
-->
<template>
  <div class="content">
    <el-card shadow="hover">
      <template #header>
        <div class="header">
          <span>友情链接管理</span>
          <el-button
            type="success"
            @click="addDialogShow = true"
            icon="CirclePlus"
            >添加链接</el-button
          >
        </div>
      </template>

      <!-- table -->
      <el-table :data="linkList" style="width: 100%">
        <el-table-column prop="id" label="id" width="40" />
        <el-table-column prop="title" label="链接标题" />
        <el-table-column prop="url" label="网站地址" />
        <el-table-column label="排序数字">
          <template #default="scope">
            <el-tag>{{ scope.row.link_order }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="create_time" label="添加日期" />
        <el-table-column label="操作" width="200">
          <template #default="scope">
            <el-button
              @click="handleRemove(scope.row, scope.$index)"
              type="danger"
              icon="Delete"
              >删除</el-button
            >
            <el-button @click="handleEdit(scope.row)" type="primary" icon="Edit"
              >编辑</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 添加 对话框 -->
    <el-dialog v-model="addDialogShow" title="添加链接">
      <el-form
        ref="addFormRef"
        :rules="rules"
        :model="addForm"
        label-position="top"
      >
        <el-form-item label="标题 (可选)" prop="title">
          <el-input type="text" v-model="addForm.title"></el-input>
        </el-form-item>
        <el-form-item label="跳转地址 (例：http://www.baidu.com)" prop="url">
          <el-input type="text" v-model="addForm.url"></el-input>
        </el-form-item>
        <el-form-item label="排序数字 (数字越小越靠前)" prop="link_order">
          <el-input-number v-model="addForm.link_order" :min="0" />
        </el-form-item>
        <el-divider></el-divider>
        <el-button
          @click="handleAddSubmit"
          type="primary"
          class="btn-1"
          icon="CirclePlus"
          style="width: 100%"
          round
          >添 加</el-button
        >
      </el-form>
    </el-dialog>

    <!-- 编辑 对话框 -->
    <el-dialog v-model="editDialogShow" title="编辑链接">
      <el-form
        ref="editFormRef"
        :rules="rules"
        :model="editForm"
        label-position="top"
      >
        <el-form-item label="标题 (可选)" prop="title">
          <el-input type="text" v-model="editForm.title"></el-input>
        </el-form-item>
        <el-form-item label="跳转地址 (例：http://www.baidu.com)" prop="url">
          <el-input type="text" v-model="editForm.url"></el-input>
        </el-form-item>
        <el-form-item label="排序数字 (数字越小越靠前)" prop="link_order">
          <el-input-number v-model="editForm.link_order" :min="0" />
        </el-form-item>
        <el-divider></el-divider>
        <el-button
          @click="handleEditSubmit"
          type="primary"
          class="btn-1"
          icon="CirclePlus"
          style="width: 100%"
          round
          >保 存</el-button
        >
      </el-form>
    </el-dialog>
  </div>
</template>
 
<script setup>
import { ref, reactive } from "vue";
import { link } from "@/api";

// 初始化
let linkList = ref((await link.list()).data);

// 表单实例
const addFormRef = ref(null);
const editFormRef = ref(null);

// 对话框显示
const addDialogShow = ref(false);
const editDialogShow = ref(false);

// 表单
const addForm = ref({
  title: "",
  url: "",
  link_order: 0,
});
const editForm = ref({});

// 验证规则
const rules = reactive({
  url: [{ required: true, message: "请输入跳转地址", trigger: "blur" }],
  title: [{ required: true, message: "请选择跳转方式", trigger: "blur" }],
  link_order: [{ required: true, message: "请输入排序数字", trigger: "blur" }],
});

/**
 * @description: 添加
 * @return {*}
 */
const handleAddSubmit = () => {
  addFormRef.value.validate(async (valid, fields) => {
    if (valid) {
      let res = await link.add(addForm.value);
      if (res.status) {
        ElMessage.success("添加成功");
        linkList.value = (await link.list()).data;
        addDialogShow.value = false;
        return;
      }
      ElMessage.error(res.msg);
      return;
    }
  });
};

/**
 * @description: 删除
 * @return {*}
 * @param {*} row
 * @param {*} index
 */
const handleRemove = (row, index) => {
  ElMessageBox.confirm(`您是否确认删除 ${row.title} 链接`, "Warning", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(async () => {
      let { status, msg } = await link.remove(row.id);
      if (status) {
        linkList.value.splice(index, 1);
      }
      ElMessage({
        type: status ? "success" : "error",
        message: msg,
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "已取消",
      });
    });
};

/**
 * @description: 打开编辑对话框
 * @return {*}
 * @param {*} row
 */
const handleEdit = (row) => {
  editForm.value = row;
  editDialogShow.value = true;
};

/**
 * @description: 编辑
 * @return {*}
 */
const handleEditSubmit = () => {
  editFormRef.value.validate(async (valid, fields) => {
    if (valid) {
      let res = await link.edit(editForm.value.id, editForm.value);
      if (res.status) {
        ElMessage.success("编辑成功");
        linkList.value = (await link.list()).data;
        editDialogShow.value = false;
        return;
      }
      ElMessage.error(res.msg);
      return;
    }
  });
};

console.log(linkList);
</script>

<style lang='less' scoped>
.header {
  display: flex;
  justify-content: space-between;
  align-content: center;
}
</style>
